package xyz.yuzegod.megawalls.classes.novice.skeleton;

import org.bukkit.Sound;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class MainSkill extends Skill
{
    public MainSkill(final Classes classes) {
        super("爆炸箭矢", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 0.1;
            }
            case 2: {
                return 0.12;
            }
            case 3: {
                return 0.14;
            }
            case 4: {
                return 0.16;
            }
            case 5: {
                return 0.18;
            }
            default: {
                return 0.1;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7发射一支爆炸箭,");
            lore.add("   §7造成敌人剩余血量§a" + StringUtils.percent(this.getAttribute(level)) + "§7的伤害,");
            lore.add("   §7并击退周围敌人。最少造成2点伤害。");
            return lore;
        }
        lore.add(" §8▪ §7发射一支爆炸箭,");
        lore.add("   §7造成敌人剩余血量§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜");
        lore.add("   §a" + StringUtils.percent(this.getAttribute(level)) + "§7的伤害,");
        lore.add("   §7并击退周围敌人。最少造成2点伤害。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final Player player = gamePlayer.getPlayer();
        final Arrow arrow = (Arrow)player.launchProjectile((Class)Arrow.class);
        arrow.setMetadata(MegaWalls.getMetadataValue(), (MetadataValue)MegaWalls.getFixedMetadataValue());
        arrow.setVelocity(player.getLocation().getDirection().multiply(2.35));
        new BukkitRunnable() {
            public void run() {
                if (arrow.isDead() || arrow.isOnGround()) {
                    ParticleEffect.EXPLOSION_HUGE.display(0.0f, 0.0f, 0.0f, 1.0f, 1, arrow.getLocation(), 10.0);
                    arrow.getWorld().playSound(arrow.getLocation(), Sound.EXPLODE, 1.0f, 0.0f);
                    final TNTPrimed tntPrimed = (TNTPrimed)player.getWorld().spawn(arrow.getLocation().add(0.0, 1.0, 0.0), (Class)TNTPrimed.class);
                    tntPrimed.setMetadata("SKELETON", (MetadataValue)new FixedMetadataValue((Plugin)MegaWalls.getInstance(), (Object)gamePlayer.getGameTeam()));
                    tntPrimed.setFuseTicks(1);
                    final int level = kitStats.getSkillLevel();
                    for (final Player player1 : PlayerUtils.getNearbyPlayers(arrow.getLocation(), 10.0)) {
                        final GamePlayer gamePlayer1 = GamePlayer.get(player1.getUniqueId());
                        if (!gamePlayer1.isSpectator()) {
                            if (gamePlayer.getGameTeam().isInTeam(gamePlayer1)) {
                                continue;
                            }
                            player1.damage(player1.getHealth() * MainSkill.this.getAttribute(level), (Entity)player);
                        }
                    }
                    if (!arrow.isDead()) {
                        arrow.remove();
                    }
                    this.cancel();
                    return;
                }
                ParticleEffect.CRIT_MAGIC.display(0.0f, 0.0f, 0.0f, 1.0f, 5, arrow.getLocation(), 10.0);
            }
        }.runTaskTimer((Plugin)MegaWalls.getInstance(), 0L, 1L);
        return true;
    }
}
